import { Storage } from '@ionic/storage';
import { Component, Injector, ViewChild } from '@angular/core';
import { IonicPage } from 'ionic-angular';
import { BasePage } from '../../basePage';
import { nailerService } from './../../../providers/nailerService';
// import { LookImgPage } from '../look-img/look-img';
import { NotePubPage } from '../note-pub/note-pub';
import { StorageService } from '../../../providers/StorageService';

import { PIC_URL } from '../../../providers/Config';
import { publish } from 'rxjs/operator/publish';
import { UserService } from './../../../providers/UserService';
/**
 * Generated class for the NoteDetailPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
	selector: 'page-note-detail',
	templateUrl: 'note-detail.html'
})
export //@ViewChild('myInput') input
	class NoteDetailPage extends BasePage {
	push_time: '';
	avter: '';
	picture: string;
	/**判断是店主还是美甲师 */
	is_person: any;
	/**提示信息 */
	msg: any;
	userinfo: any;
	/** */
	nick_name = '';
	presenter_info: any;
	/**获取文章详情的参数 */
	myinput2: string = '';
	textDetailParamsObj = {
		presenter_id: '',
		article_id: ''
	};
	/**文章详情 */
	textDetail = [];
	/**评论 */
	usId: number;
	myInput: string = '';
	publishObj = {
		review_content: '',
		writer_id: '',
		article_id: '',
		user_id: ''
	};
	usT = {
		review_content: '',
		writer_id: '',
		user_id: '',
		article_id: '',
		to_id: 0
	};
	UsName: string;
	GetUserT: string;
	show: boolean = true;
	hide: boolean = false;
	/**获取当前的评论数 */
	public comm_count: string;
	constructor(
		injector: Injector,
		private nailerservice: nailerService,
		private userService: UserService,
		public storage: StorageService
	) {
		super(injector);
		this.picture = PIC_URL;
	}
	ionViewDidLoad() {
		console.log('ionViewDidLoad NoteDetailPage');
	}
	ionViewWillEnter() {
		this.publishObj.article_id = this.textDetailParamsObj.article_id = this.navParams.get('txt_id');
		this.publishObj.writer_id = this.textDetailParamsObj.presenter_id = this.navParams.get('presenter_id');
		this.userinfo = this.storage.read('userinfo');
		if (this.userinfo != null) {
			this.publishObj.user_id = this.userinfo.user_id;
		}
		console.log(this.textDetailParamsObj.presenter_id);
		this.getTextDetail();
	}
	/**点击返回到上一页面 */
	dismiss() {
		let data = { foo: 'bar' };
		this.viewCtrl.dismiss(data);
	}
	/**获取文章详情 */
	async getTextDetail() {
		let res = await this.nailerservice.getTextDetail(this.textDetailParamsObj);
		console.log(res);
		if (res.data != '') {
			this.textDetail = res.data;
			console.log(this.textDetail['reviewer_list']);
			this.textDetail['reviewer_list'] = this.textDetail['reviewer_list'].reverse();
			this.comm_count = this.textDetail['reviewer_list'].length;
		}

		if (res.data.presenter_info.shop_id) {
			this.nick_name = res.data.presenter_info.shop_name;
			this.avter = res.data.presenter_info.shop_logo;
			this.push_time = res.data.push_time;
		} else {
			this.presenter_info = res.data.presenter_info;
			this.is_person = res.data.presenter_info.shop_id;
			this.nick_name = this.presenter_info.artist_nickname;
			this.avter = this.presenter_info.artist_avator;
			this.push_time = res.data.push_time;
		}
	}
	/**点击发布文章 */
	publish() {
		this.navCtrl.push('NotePubPage');
	}
	/**查看图片 */
	scaleModalScalePage(url) {
		this.modalCtrl
			.create(
				'LookImgPage',
				{ imgurl: url },
				{
					enterAnimation: 'modal-scale-enter',
					leaveAnimation: 'modal-scale-leave'
				}
			)
			.present();
	}
	/**发布评论 */
	async publishArticleComment() {


		if (this.userinfo == null) {
			let alert = this.alertCtrl.create({
				title: "登录提示",
				subTitle: "未登录，请先登陆",
				buttons: [
					{
						text: "取消",
						handler: data => { }
					},
					{
						text: "确认登录",
						handler: data => {
							let modal = this.modalCtrl.create("LoginPage", { reload: true });
							modal.onDidDismiss(data => {

							});
							modal.present();
						}
					}
				]
			});
			alert.present();
			return;
		}


		console.log(this.myInput);
		this.publishObj.review_content = this.myInput;
		console.log(this.publishObj);
		let res = await this.nailerservice.publishArticleComment(this.publishObj);
		console.log(res);
		if (res.error_code == 0) {
			this.getTextDetail();
			this.myInput = '';
			this.msg = '发表评论成功';
			this.presentToast();
		} else {
			this.msg = res.msg;
			this.presentToast();
		}
	}
	// dle() {
	// 	delete this.publishObj.to_id;
	// }
	/**回复评论 获取用户ID */

	clickUser(i, j, UserName: HTMLInputElement, GetUser: HTMLInputElement) {
		this.UsName = this.textDetail['reviewer_list'][i].reviewer_nickname;
		this.usId = this.textDetail['reviewer_list'][i].id;
		let comment = this.textDetail['reviewer_list'][i];
		let reply = comment.reply_comment;
		if (this.userinfo != null) {
			var usId = this.userinfo.user_id;
		}
		if (reply != undefined) {
			let num = comment.reply_comment.length;
			for (let j = 0; j < num; j++) {
				if (reply[j].reviewer_id == usId) {
					let alert = this.alertCtrl.create({
						title: '提示信息',
						subTitle: '您已经评论过了，无法再次评论',
						buttons: ['确定']
					});
					alert.present();
					this.hide = false;
					this.show = true;
				} else {
					this.show = false;
					this.hide = true;
				}
			}
		} else {
			this.show = false;
			this.hide = true;
		}
		//时节分流
		// let  sendMessage=()=>{
		// }
		// let  start = setTimeout(()=>
		// {
		// if(start){
		// clearTimeout(start);
		// 	start= setTimeout(()=>sendMessage(),500);
		// }else{
		// 	sendMessage()
		// }
		// },500)
	}

	/*发送回复请求 */
	async getUser(GetUser: HTMLInputElement) {
		this.GetUserT = GetUser.value;
		this.usT.review_content = this.GetUserT;
		this.usT.to_id = this.usId;
		this.usT.article_id = this.textDetailParamsObj.article_id = this.navParams.get('txt_id');
		this.usT.writer_id = this.textDetailParamsObj.presenter_id = this.navParams.get('presenter_id');
		if (this.userinfo != null) {
			this.usT.user_id = this.userinfo.user_id;
		}

		console.log(this.usT);
		console.log(this.publishObj);
		let res = await this.userService.postUserT(this.usT);
		console.log(res);

		if (res.error_code == 0) {
			this.getTextDetail();
			this.GetUserT = '';
			this.msg = '回复评论成功';
			this.presentToast();
			this.hide = false;
			this.show = true;
		} else {
			this.msg = res.msg;
			this.presentToast();
		}
	}

	ngHide(imputbx: HTMLInputElement, GetUser: HTMLInputElement) {
		// this.hide = false;
		// this.show = true;
		// delete this.publishObj.to_id;
		console.log('失去焦点');
	}
	Bghide() {
		this.hide = false;
		this.show = true;
		// delete this.publishObj.to_id;
	}

	presentToast() {
		let toast = this.toastCtrl.create({
			message: this.msg,
			duration: 2000,
			position: 'top'
		});
		toast.onDidDismiss(() => {
			console.log('Dismissed toast');
		});

		toast.present();
	}
}
